package me.bincker.formwork.web.core.service.impl

import me.bincker.formwork.web.core.service.IUserDetailsService
import me.bincker.formwork.web.modules.system.entity.SysUserDetails
import me.bincker.formwork.web.modules.system.entity.User
import me.bincker.formwork.web.modules.system.repository.IUserRepository
import org.springframework.data.domain.Example
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.stereotype.Service

@Service
class UserDetailsServiceImpl(
        private val repository: IUserRepository
) : IUserDetailsService {
    override fun loadUserByUsername(username: String): UserDetails {
        val user = repository.findOne(Example.of(User().apply { this.username = username }))
        if(user.isPresent){
            return SysUserDetails(user.get())
        }else throw UsernameNotFoundException("用户[${username}]不存在")
    }
}